import axios from 'axios'
import { getToken } from '@neu/biz-core/utils/auth'

const store = {
  promiseList: [],
  fetch(url) {
    const that = this
    const cachePromise = that.checkPromise(url)
    if (cachePromise) {
      return cachePromise
    }

    const promise = axios
      .get(url, { headers: { 'Authentication': getToken() }})

    that.promiseList.push({
      key: url,
      value: promise
    })
    return promise
  },
  checkPromise(url) {
    const key = url
    const res = this.promiseList.filter(item => item.key === key)
    if (res.length > 0) {
      return res[0].value
    } else {
      return false
    }
  }
}

export default store
